// miniprogram/dati/dati.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 'startPage',
    currentTimu:{},
    currentNum:0,
    timuList :[],
    score: 0,
    styleArr: ["optionsItem","optionsItem","optionsItem","optionsItem"],
    isAnswer:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {

    let  result = await this.getTimu()
    // console.log(result);
    console.log(result.data[0]);
    
    this.setData ({
      timuList: result.data,
      currentTimu: result.data[0]
    })
    this.getRange()
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  async getTimu(){
    let randoNum = parseInt(Math.random()*16000)
    let db = wx.cloud.database();
    //skip设置偏移的数量
  let result = await db.collection('timu').skip(randoNum).limit(10).get()
  return result
  },
  goGaming(){ 
    this.setData({
      page:'gamPage',   
    })
  },
  chooseEvent(e){
  
    let that = this;
    console.log(e);
    // console.log('已执行');
    
    if (this.data.isAnswer) {
      return;
    }
    let num = e.currentTarget.dataset.num;
    if (num == this.data.currentTimu.answer) {
      let score = this.data.score += 10;
      //一个数组搜集样式
     let arr = this.data.styleArr;
     arr[num - 1] = 'optionsItem success';
   let currentNum = this.data.currentNum + 1 == 10 ? 0 :this.data.currentNum + 1 
     this.setData({
       styleArr: arr,
       score: score,
       currentNum: currentNum,
       isAnswer:true,
     })
     //答对走下一题
     setTimeout(()=>{
      this.setData({
        currentTimu: this.data.timuList[this.data.currentNum],
        styleArr: ["optionsItem","optionsItem","optionsItem","optionsItem"],
        isAnswer: false
      })
     },2000)
   }else{
      let arr = this.data.styleArr;
      arr[num -1] = 'optionsItem fail'
      arr[this.data.currentTimu.answer -1 ] = 'optionsItem success';
      this.setData({
        styleArr: arr,
        isAnswer:true
      })
      //点击失败就跳出endPage页面
      setTimeout(()=>{
        this.setData({
          page: 'endPage',
          isAnswer:false
        })
        wx.getUserInfo({
          success(res){
            // console.log(res);
          let data = res.userInfo;
            data.score = that.data.score;
            let db = wx.cloud.database();
            db.collection('datiRange').add({
              data: data,
              success(e){
                console.log(e);
                that.getRange()
                
              }
            })
            
          }
        })
      },2000)
  }

 },

  async getRange(){
  let db = wx.cloud.database();
  let res = await db.collection('datiRange').aggregate() .sort({
    score: -1
  }).limit(3).end()
  this.setData({
    rangeList:res.list
  })
  // console.log(res);
  
 }
    

})